package com.zhang.family_doctor.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.family_doctor.entity.Mechanism;
import com.zhang.family_doctor.entity.R;
import com.zhang.family_doctor.mapper.MechanismMapper;
import com.zhang.family_doctor.service.MechanismService;
import com.zhang.family_doctor.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;

@Service
public class MechanismServiceImpl implements MechanismService {

    @Autowired
    private MechanismMapper mechanismMapper;

    /**
     * 新增机构
     * @param mechanism
     * @return
     */
    @Override
    public R<String> save(Mechanism mechanism) {

        String phone = mechanism.getPhone();

        if (phone != null & phone != "") {
            //校验手机号
            Matcher matcher = CommonUtils.getPhone(phone);
            boolean matchesPhone = matcher.matches();
            if (matchesPhone) {
                mechanism.setId(CommonUtils.genUUID());
                mechanism.setNumber(CommonUtils.genUUID());
                //默认新增启用
                mechanism.setState(1);
                mechanism.setCreateTime(new Date());
                mechanism.setUpdateTime(new Date());
                mechanismMapper.save(mechanism);
                return R.success("新增成功！");
            } else {
                return R.buildFailure("手机号格式不对！请重新输入有效的手机号！");
            }
        }else {
            return R.buildFailure("手机号不能为空！");
        }
    }

    /**
     * 修改机构
     * @param mechanism
     * @return
     */
    @Override
    public R<String> update(Mechanism mechanism) {

        String phone = mechanism.getPhone();
        if ( phone == null && phone == ""){
            return R.buildFailure("手机号不能为空！");
        }
        //校验手机号
        Matcher matcher = CommonUtils.getPhone(phone);
        boolean matches = matcher.matches();
        if (matches){

            mechanism.setUpdateTime(new Date());
            mechanismMapper.update(mechanism);
            return R.success("修改成功！");

        }else {
            return R.buildFailure("该手机号格式不对！请换一个手机号！");
        }
    }

    /**
     * 分页查询机构
     * @param pageNum
     * @param pageSize
     * @param companyName
     * @return
     */
    @Override
    public R<PageInfo<Mechanism>> getList(Integer pageNum, Integer pageSize, String companyName) {

        PageHelper.startPage(pageNum, pageSize);
        List<Mechanism> mechanismList = mechanismMapper.getList(companyName);
        PageInfo<Mechanism> pageInfo = new PageInfo<>(mechanismList);
        return R.success(pageInfo);
    }

    /**
     * 根据id删除机构
     * @param id
     * @return
     */
    @Override
    public R<String> delete(Integer id) {

        mechanismMapper.delete(id);

        return R.success("删除成功！");
    }

    /**
     * 禁用或启用
     * @param id
     * @param state
     * @return
     */
    @Override
    public R<String> updateStateById(Integer id, Integer state) {

        mechanismMapper.updateStateById(id,state);

        return R.success("修改成功！");
    }
}
